<?php

namespace app\model;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

/**
 * @mixin think\Model
 */
class UserModel extends Model
{
    protected $table = 'tp_user';
    protected $pk = 'user_id';//主键

    /**
     * 获取单条数据
     * @param string $trWhere
     * @return array|Model|null
     */
    public function getOne($trWhere = '') {
        try {
            return $this ::where($trWhere)->find();
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
        }

    }

    /**
     * 添加数据
     * @param array $getData
     * @return bool
     */
    public  function getCreate($getData = []) {
        $userModel = new self();
        $userModel->username = $getData['username'];
        $userModel->password = $getData['password'];
        $userModel->create_time = time();

        return $userModel->save();
    }

    /**
     * 修改数据
     * throws \think\db\exception\ModelNotFoundException
     */
    public  function getUpdate($getData = []) {
        try {
            $userModel = UserModel::find($getData['user_id']);
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
        }
        $userModel->username = $getData['username'];
        $userModel->password = $getData['password'];
        $userModel->update_time = time();
        /** @var TYPE_NAME $userModel */
        return $userModel->save();

    }
}
